package com.yanfan.alarm;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.alarm.domain.NjAlarmDeviceInfo;
import com.yanfan.alarm.domain.NjAlarmRecord;
import com.yanfan.alarm.domain.NjDefenceAreaInfo;
import com.yanfan.alarm.service.INjAlarmDeviceInfoService;
import com.yanfan.alarm.service.INjAlarmRecordService;
import com.yanfan.alarm.service.INjDefenceAreaInfoService;
import com.yanfan.alarm.utils.OauthUtil;
import com.yanfan.ibms.common.utils.HttpUtils2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Component("alarmTask")
public class ApiAlarmController {

    @Value("${api.dahua.url}")
    private String url;

    @Resource
    private INjAlarmDeviceInfoService alarmDeviceInfoService;
    @Resource
    private INjDefenceAreaInfoService defenceAreaInfoService;
    @Resource
    private INjAlarmRecordService alarmRecordService;

    /**
     * 报警主机状态
     * @throws Exception
     */
    public void getAlarmStatus() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendPostSSLBody(
                        url +
                                "/evo-apigw/evo-alarm/1.0.0/alarmhosts/all"
                        ,headers,param
                );
        List<NjAlarmDeviceInfo> list = JSON.parseArray(JSONObject.parseObject(jsonObject.
                get("data").toString()).get("value").toString(), NjAlarmDeviceInfo.class);
        alarmDeviceInfoService.saveOrUpdateBatch(list);
    }


    /**
     * 子系统报警状态  防区状态
     * @throws Exception
     */
    public void getSalveAlarmStatus() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("deviceCode","1000264");
        //param.put("pageSize",9999);
        JSONObject jsonObject = HttpUtils2.
                sendSLLGetParm(
                        url +
                                "/evo-apigw/evo-alarm/1.0.0/alarmhosts/one/1000264"
                        ,headers,null);
        JSONArray parseArray = JSON.parseArray(JSONObject.parseObject(jsonObject.get("data").toString())
                .get("value").toString());

        parseArray.stream().forEach(alarmMaster ->{
            List<NjDefenceAreaInfo> defenceAreaList = JSON.parseArray(JSONObject.parseObject(alarmMaster.toString())
                    .get("defenceAreaList").toString(), NjDefenceAreaInfo.class);
            defenceAreaInfoService.saveOrUpdateBatch(defenceAreaList);
        });
    }

    /**
     * 报警事件记录
     * @throws Exception
     */
    public void getAlarmRecord() throws Exception {
        String publicKey = OauthUtil.getPublicKey();
        String accessToken = OauthUtil.accessToken(publicKey);
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization","bearer " + accessToken);
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum",1);
        param.put("pageSize",999);
        //param.put("alarmType",81);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 获取十分钟前的时间
        LocalDateTime tenMinutesAgo = now.minusMinutes(5);
        // 格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime = now.format(formatter);
        String tenMinutesAgoTime = tenMinutesAgo.format(formatter);
        param.put("alarmStartDateString",nowTime);
        param.put("alarmEndDateString",tenMinutesAgoTime);
        JSONObject jsonObject = HttpUtils2.
                sendPostSSLBody(
                        url + "/evo-apigw/evo-event/1.2.0/alarm-record/page"
                        ,headers,param);
        List<NjAlarmRecord> parseArray = JSON.parseArray(JSONObject.parseObject(jsonObject.get("data").toString())
                .get("pageData").toString(), NjAlarmRecord.class);
        alarmRecordService.saveOrUpdateBatch(parseArray);
    }


}
